home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / aaline.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.0 KB  |  282 lines

  1. /*
  2. ** Copyright 1994, Silicon Graphics, Inc.
  3. ** All Rights Reserved.
  4. **
  5. ** This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6. ** the contents of this file may not be disclosed to third parties, copied or
  7. ** duplicated in any form, in whole or in part, without the prior written
  8. ** permission of Silicon Graphics, Inc.
  9. **
  10. ** RESTRICTED RIGHTS LEGEND:
  11. ** Use, duplication or disclosure by the Government is subject to restrictions
  12. ** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13. ** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14. ** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15. ** rights reserved under the Copyright Laws of the United States.
  16. **
  17. */
  18. #include <GL/glx.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <X11/keysym.h>
  24.  
  25. static int RGB_attributes[] = {
  26.     GLX_RGBA,
  27.     GLX_RED_SIZE, 1,
  28.     GLX_GREEN_SIZE, 1,
  29.     GLX_BLUE_SIZE, 1,
  30.     None,
  31. };
  32.  
  33. static int CI_attributes[] = {
  34.     None,
  35. };
  36.  
  37. int rgb = 1;
  38. int W = 401;
  39. int H = 401;
  40.  
  41. float rotangle = 0;
  42. float width = 1;
  43. int stippling = 0;
  44. int anti = 1;
  45.  
  46. static void Init(void)
  47. {
  48.     glViewport(0,0,W,H);
  49.     glMatrixMode(GL_PROJECTION);
  50.     glLoadIdentity();
  51.     glOrtho(-100.0, 100.0, -100.0, 100.0, 0.0, 1.0);
  52.     glMatrixMode(GL_MODELVIEW);
  53.  
  54.     if (rgb) {
  55.     glClearColor(0.15,0.15,0.3,0);
  56.     } else {
  57.     glClearIndex(16);
  58.     }
  59.  
  60.     glClear(GL_COLOR_BUFFER_BIT);
  61. }
  62.  
  63. static void Redraw(void)
  64. {
  65.     glClear(GL_COLOR_BUFFER_BIT);
  66.     glLineWidth(width);
  67.  
  68.     if (rgb) {
  69.     glColor3f(1.0, 1.0, 1.0);
  70.     } else {
  71.     if (anti) {
  72.         glIndexf(0);
  73.     } else {
  74.         glIndexf(15);
  75.     }
  76.     }
  77.     glPushMatrix();
  78.     glRotatef(rotangle, 0, 0, 1);
  79.     glBegin(GL_LINES);
  80.     glVertex3i(-90, 0, 0);
  81.     glVertex3i(90, 0, 0);
  82.     glEnd();
  83.     glPopMatrix();
  84.     glFlush();
  85. }
  86.  
  87. static void Usage(void)
  88. {
  89.     fprintf(stderr, "Usage: program [-c]\n");
  90.     exit(1);
  91. }
  92.  
  93. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  94. {
  95.     if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
  96.     return GL_TRUE;
  97.     }
  98.     return GL_FALSE;
  99. }
  100.  
  101. int main(int argc, char** argv)
  102. {
  103.     XVisualInfo *vi;
  104.     Display *dpy;
  105.     Colormap cmap;
  106.     Window window;
  107.     XSetWindowAttributes swa;
  108.     GLXContext cx;
  109.     XEvent event;
  110.     GLboolean needDisplay;
  111.     XColor white;
  112.     int i;
  113.  
  114.     rgb = 1;
  115.     for (i = 1; i < argc; i++) {
  116.         if (argv[i][0] == '-') {
  117.             switch (argv[i][1]) {
  118.               case 'c':
  119.                 rgb = GL_FALSE;
  120.                 break;
  121.               default:
  122.                 Usage();
  123.             }
  124.         } else {
  125.             Usage();
  126.         }
  127.     }
  128.  
  129.     dpy = XOpenDisplay(0);
  130.     if (!dpy) {
  131.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  132.     return -1;
  133.     }
  134.  
  135.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  136.              rgb ? RGB_attributes : CI_attributes);
  137.     if (!vi) {
  138.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  139.         getenv("DISPLAY"));
  140.     return -1;
  141.     }
  142.  
  143.     if (rgb) {
  144.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  145.                    AllocNone);
  146.     white.red = ~0;
  147.     white.green = ~0;
  148.     white.blue = ~0;
  149.     XAllocColor(dpy, cmap, &white);
  150.     swa.background_pixel = white.pixel;
  151.     } else {
  152.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  153.                    AllocAll);
  154.     swa.background_pixel = 15;
  155.     }
  156.  
  157.     swa.border_pixel = 0;
  158.     swa.colormap = cmap;
  159.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  160.     | KeyReleaseMask;
  161.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  162.                W, H,
  163.                0, vi->depth, InputOutput, vi->visual,
  164.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  165.                &swa);
  166.     XSetStandardProperties(dpy, window, "OpenGL Antialiased Line demo", "aaline",
  167.                None, argv, argc, NULL);
  168.     XSetWMColormapWindows(dpy, window, &window, 1);
  169.     XMapWindow(dpy, window);
  170.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  171.  
  172.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  173.     if (!glXMakeCurrent(dpy, window, cx)) {
  174.     fprintf(stderr, "Can't make window current to context\n");
  175.     return -1;
  176.     }
  177.  
  178.     if (!rgb) {
  179.     XColor buf;
  180.     int i;
  181.  
  182.     buf.flags = DoRed | DoGreen | DoBlue;
  183.  
  184.     /* Init color map */
  185.     for (i=0; i<16; i++) {
  186.         buf.pixel = i;
  187.         buf.red = i*(65535/15);
  188.         buf.green = i*(65535/15);
  189.         buf.blue = i*(65535/15);
  190.         XStoreColor(dpy, cmap, &buf);
  191.     }
  192.     buf.pixel = 16;
  193.     buf.red = 10000;
  194.     buf.green = 10000;
  195.     buf.blue = 20000;
  196.     XStoreColor(dpy, cmap, &buf);
  197.     }
  198.  
  199.     Init();
  200.     glEnable(GL_LINE_SMOOTH);
  201.     if (rgb) {
  202.     glEnable(GL_BLEND);
  203.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  204.     }
  205.  
  206.     needDisplay = GL_TRUE;
  207.     for (;;) {
  208.     do {
  209.         XNextEvent(dpy, &event);
  210.         switch (event.type) {
  211.           case Expose:
  212.         needDisplay = GL_TRUE;
  213.         break;
  214.           case ConfigureNotify:
  215.         W = event.xconfigure.width;
  216.         H = event.xconfigure.height;
  217.         needDisplay = GL_TRUE;
  218.         break;
  219.           case KeyPress:
  220.         {
  221.             char buf[100];
  222.             int rv;
  223.             KeySym ks;
  224.             GLboolean setNeed = GL_TRUE;
  225.  
  226.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  227.             switch (ks) {
  228.               case XK_O: case XK_o:
  229.             rotangle += 1;
  230.             break;
  231.               case XK_P: case XK_p:
  232.             rotangle -= 1;
  233.             break;
  234.               case XK_Q: case XK_q:
  235.             width -= 0.125;
  236.             if (width < 1) width = 1;
  237.             break;
  238.               case XK_W: case XK_w:
  239.             width += 0.125;
  240.             break;
  241.               case XK_A: case XK_a:
  242.             anti = 1-anti;
  243.             if (anti) {
  244.                 glEnable(GL_LINE_SMOOTH);
  245.                 if (rgb) {
  246.                 glEnable(GL_BLEND);
  247.                 }
  248.             } else {
  249.                 glDisable(GL_LINE_SMOOTH);
  250.                 if (rgb) {
  251.                 glDisable(GL_BLEND);
  252.                 }
  253.             }
  254.             break;
  255.               case XK_L: case XK_l:
  256.             stippling = 1-stippling;
  257.             if (stippling) {
  258.                 glEnable(GL_LINE_STIPPLE);
  259.                 glLineStipple(2, 0xf090);
  260.             } else {
  261.                 glDisable(GL_LINE_STIPPLE);
  262.             }
  263.             break;
  264.               case XK_Escape:
  265.             return 0;
  266.               default:
  267.             setNeed = GL_FALSE;
  268.             break;
  269.             }
  270.             if (setNeed) needDisplay = GL_TRUE;
  271.         }
  272.         break;
  273.         }
  274.     } while (XPending(dpy) != 0);
  275.  
  276.     if (needDisplay) {
  277.         needDisplay = GL_FALSE;
  278.         Redraw();
  279.     }
  280.     }
  281. }
  282.